﻿using AdverseEventReportSystem.BLL;
using AdverseEventReportSystem.BLL.ServiceReference1;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Model;
using AdverseEventReportSystem.Service;
using AdverseEventReportSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AdverseEventReportSystem.Web.Areas.AdverseEvent
{
    public class EditPressureSoreEventController : BaseController
    {
        // GET: AdverseEvent/EditPressureSoreEvent
        public ActionResult Index(int id = 0, int pageType = 0, string detailsViewBtns = "", int status = 0)
        {
            ViewBag.EventID = id;
            ViewBag.PageType = pageType;
            ViewBag.CurrDepCode = loginUser.CurrDepCode;
            ViewBag.DetailsViewBtns = detailsViewBtns;
            ViewBag.Status = status;
            return View();
        }

        /// <summary>
        /// 添加/编辑不良事件
        /// </summary>
        /// <param name="adverseEventInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save(AdverseEventInfo adverseEventInfo)
        {
            Model.AdverseEvent adverseEvent = adverseEventInfo.AdverseEvent;
            adverseEvent.Flag_Yc = "1";
            adverseEventInfo.NurseList = new List<AdverseEventNurse>() { new AdverseEventNurse() { NurseID = loginUser.Personnel_id, NurseName = loginUser.Name } };
            OperateResult resultInfo = AdverseEventManager.Save(adverseEventInfo);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 压疮(压伤)反馈
        /// </summary>
        /// <returns></returns>
        public ActionResult YcFanKui(int eventId)
        {
            AdverseEventYcFanKui adverseEventYcFanKui = new AdverseEventYcFanKui();
            if (eventId > 0)
            {
                adverseEventYcFanKui = AdverseEventYcFanKuiService.GetQuerySingleByEventID(eventId);
                if (adverseEventYcFanKui == null) adverseEventYcFanKui = new AdverseEventYcFanKui();
            }
            adverseEventYcFanKui.EventId = eventId;
            ViewBag.AdverseEventYcFanKui = adverseEventYcFanKui;
            return View();
        }

        /// <summary>
        /// 获取压疮(压伤)反馈
        /// </summary>
        /// <returns></returns>
        public ActionResult GetYcFanKui(int eventId)
        {
            AdverseEventYcFanKui adverseEventYcFanKui = AdverseEventYcFanKuiService.GetQuerySingleByEventID(eventId);
            return JsonDate(adverseEventYcFanKui);
        }

        /// <summary>
        /// 添加/编辑压疮(压伤)反馈
        /// </summary>
        /// <param name="ycFanKui"></param>
        /// <param name="oldStatus"></param>
        /// <returns></returns>
        public ActionResult YcFanKuiSave(AdverseEventYcFanKui ycFanKui, int oldStatus)
        {
            OperateResult resultInfo = AdverseEventManager.YcFanKuiSave(ycFanKui, oldStatus);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 打印报表
        /// </summary>
        /// <returns></returns>
        public ActionResult Report(int eventID)
        {
            ViewBag.EventID = eventID;
            return View();
        }

        /// <summary>
        /// 获取报表数据
        /// </summary>
        /// <param name="eventID"></param>
        /// <returns></returns>
        [LoginAttribute(IsNeed = false)]
        public ActionResult GetReportData(int eventID)
        {
            DataTable dt = AdverseEventService.GetAdverseEventTable(eventID);
            dt.Columns.Add("ReportDate");
            dt.Columns.Add("Score1");
            dt.Columns.Add("Score2");
            dt.Columns.Add("Score3");
            dt.Columns.Add("Score4");
            dt.Columns.Add("Score5");
            dt.Columns.Add("Score6");
            dt.Columns.Add("TotalScore");
            dt.Columns.Add("Ycly_Yw");
            dt.Columns.Add("Ycly_Yn");
            dt.Columns.Add("Ycly_Mark");

            if (eventID == 0)
            {
                return JsonDate(new { EventInfo = dt });
            }

            DataRow dr = dt.Rows[0];
            dr["ReportDate"] = DataConvert.StrToDateTime(dr["CreateTime"], DateTime.Now).ToString("yyyy-MM-dd");
            dr["Score1"] = "0";
            dr["Score2"] = "0";
            dr["Score3"] = "0";
            dr["Score4"] = "0";
            dr["Score5"] = "0";
            dr["Score6"] = "0";
            dr["TotalScore"] = "0";

            DataTable modeDt = AdverseEventModeRecordService.GetAdverseEventModeRecordTable(eventID, DataConvert.StrToInt(dr["EventTypeID"]));
            foreach (DataRow modeDr in modeDt.Rows)
            {
                if (modeDr["ModeName"].ToString() == "压伤类型" || modeDr["ModeName"].ToString() == "感觉" || modeDr["ModeName"].ToString() == "潮湿" || modeDr["ModeName"].ToString() == "活动能力" || modeDr["ModeName"].ToString() == "移动能力" || modeDr["ModeName"].ToString() == "营养" || modeDr["ModeName"].ToString() == "摩擦力")
                {
                    string[] options = modeDr["Options"].ToString().Split('#');
                    if (modeDr["Value"].ToString() == "") continue;
                    string[] values = modeDr["Value"].ToString().Split('#');

                    if (modeDr["ModeName"].ToString() == "压伤类型")
                    {
                        for (int i = 0; i < options.Length; i++)
                        {
                            string[] ycValue = values[i].ToString().Split(new string[] { "$0%2*" }, StringSplitOptions.None);
                            if (DataConvert.StrToInt(ycValue[0]) == 1)
                            {
                                if (i == 0)
                                {
                                    dr["Ycly_Yw"] = "1";
                                }
                                else
                                {
                                    dr["Ycly_Yn"] = "1";
                                    OperateResult resultInfo = DepartmentManager.GetDetaildep(ycValue[1]);
                                    if (resultInfo.Result == 1)
                                    {
                                        dr["Ycly_Mark"] = (resultInfo.Body as tb_Department).DepartmentName;
                                    }
                                }
                            }
                        }

                    }
                    else
                    {
                        for (int i = 0; i < options.Length; i++)
                        {
                            if (DataConvert.StrToInt(values[i]) == 1)
                            {
                                switch (modeDr["ModeName"].ToString())
                                {
                                    case "感觉":
                                        dr["Score1"] = DataConvert.StrToInt(dr["Score1"]) + DataConvert.StrToInt(options[i].Split('☆')[2]);
                                        break;
                                    case "潮湿":
                                        dr["Score2"] = DataConvert.StrToInt(dr["Score2"]) + DataConvert.StrToInt(options[i].Split('☆')[2]);
                                        break;
                                    case "活动能力":
                                        dr["Score3"] = DataConvert.StrToInt(dr["Score3"]) + DataConvert.StrToInt(options[i].Split('☆')[2]);
                                        break;
                                    case "移动能力":
                                        dr["Score4"] = DataConvert.StrToInt(dr["Score4"]) + DataConvert.StrToInt(options[i].Split('☆')[2]);
                                        break;
                                    case "营养":
                                        dr["Score5"] = DataConvert.StrToInt(dr["Score5"]) + DataConvert.StrToInt(options[i].Split('☆')[2]);
                                        break;
                                    case "摩擦力":
                                        dr["Score6"] = DataConvert.StrToInt(dr["Score6"]) + DataConvert.StrToInt(options[i].Split('☆')[2]);
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }
                        dr["TotalScore"] = DataConvert.StrToInt(dr["Score1"]) + DataConvert.StrToInt(dr["Score2"]) + DataConvert.StrToInt(dr["Score3"]) + DataConvert.StrToInt(dr["Score4"]) + DataConvert.StrToInt(dr["Score5"]) + DataConvert.StrToInt(dr["Score6"]);
                    }
                }
            }

            var result = new { EventInfo = dt };
            return JsonDate(result);
        }

        /// <summary>
        /// 打印报表
        /// </summary>
        /// <returns></returns>
        public ActionResult EventPrint(int eventID)
        {
            ViewBag.EventID = eventID;
            return View();
        }
    }
}